<template>
  <div class="alarm">
    <div class="container">
      <div class="switch">
        <div class="add-alarm">

          <el-button type="primary" icon="el-icon-plus" @click="addVisible = true" round>新增警报</el-button>
          <!-- 弹出新增框 -->
          <el-dialog title="新增警报" v-model="addVisible" width="30%">
            <el-form ref="addFormRef" :model="addAlarmForm" label-width="100px">
              <el-form-item label="警报类型">
                <el-select v-model="addAlarmForm.type" placeholder="请选择警报类型">
                  <el-option label="非法占用" value="ILLEGAL_OCCUPATION"></el-option>
                  <el-option label="超时" value="OVERTIME"></el-option>
                  <el-option label="充电桩故障" value="ERROR_PILE"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="警报详情">
                <el-input v-model="addAlarmForm.alertDetail"></el-input>
              </el-form-item>
            </el-form>
            <template #footer>
              <span class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleAddAlarm">确 定</el-button>
              </span>
            </template>
          </el-dialog>
        </div>
        <div class="switch-select">
          <el-switch v-model="queryInfo.solved" active-text="是否解决" @change="getAlarmList">
          </el-switch>
        </div>
      </div>
      <div class="table">
        <el-table :data="alarmList" @sort-change="SortChange" @filter-change="filterChange" border class="table" ref="multipleTable" header-cell-class-name="table-header" stripe>
          <el-table-column label="警报编号" sortable="custom" align="center">
            <template #default="scope">{{ scope.row.alertId }}</template>
          </el-table-column>

          <el-table-column label="警报时间" align="center">
            <template #default="scope">{{ scope.row.alertDate }}</template>
          </el-table-column>

          <el-table-column label="警报内容" align="center">
            <template #default="scope">{{ scope.row.alertDetail }}</template>
          </el-table-column>

          <el-table-column label="操作" align="center">
            <template #default="scope">
              <el-button type="primary" @click="handleAlarm(scope.$index, scope.row)">解决警报</el-button>
            </template>
          </el-table-column>
        </el-table>

        <div class="pagination">
          <el-pagination background layout="total, prev, pager, next" :current-page="page.pageIndex" :page-size="page.pageSize" :total="page.totalElements" @current-change="handlePageChange">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        solved: false,
        page: 1,
        pageSize: 5,
        asc: true,
      },
      alarmList: [],
      addAlarmForm: {
        type: "",
        alertDetail: "",
      },
      page: {
        pageIndex: 1,
        pageSize: 5,
        pageTotal: 1,
        totalElements: 0,
      },
      addVisible: false,
    };
  },
  created() {
    this.getAlarmList();
  },
  methods: {
    async getAlarmList() {
      // 根据分页获取对应的警报消息
      const res = await this.$axios.get(
        `alert/admin/${this.queryInfo.solved}/${this.queryInfo.page}/${this.queryInfo.pageSize}/${this.queryInfo.asc}`
      );
      this.alarmList = res.content;
      this.page.pageIndex = res.number + 1;
      this.page.pageSize = res.size;
      this.page.pageTotal = res.totalPages;
      this.page.totalElements = res.totalElements;
      // console.log(this.alarmList);
    },
    handleAddAlarm() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        const { message: res } = await this.$axios.post(
          "/alert/admin",
          this.addAlarmForm
        );
        console.log(res);
        if (res !== "新增警报成功") return this.$message.error("新增警报失败");
        this.$message.success("新增警报成功");
        this.addVisible = false;
        this.getAlarmList();
        this.addAlarmForm.type = "";
        this.addAlarmForm.alertDetail = "";
      });
    },
    async handleAlarm(index, row) {
      // console.log(row);
      if (row.solved == true) {
        this.$message.info("该警报已经解决!");
        return;
      }
      const res = await this.$axios
        .put(`/alert/admin/${row.alertId}`)
        .then((res) => {
          if (res.message != "警报解除成功")
            return this.$message.error("警报解除失败");
          this.$message.success("警报解除成功");
          this.getAlarmList();
        })
        .catch((error) => {
          console.warn(error);
          this.$message.error("解除失败");
        });
    },
    handlePageChange(newPage) {
      this.queryInfo.page = newPage;
      this.getAlarmList();
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  min-height: 469px;
  padding: 20px;
}
.switch {
  margin-bottom: 30px;
  position: relative;
  .switch-select {
    position: absolute;
    right: 10px;
  }
}
.detail-table {
  width: 90%;
}
</style>
